library ls_loading;

import 'package:flutter/material.dart';
import 'package:ls_loading/LoadingAnimation.dart';
import 'package:ls_loading/Type.dart';

class LsLoading extends StatefulWidget {
  final LoadingStyle style;
  LsLoading({required this.style});
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<LsLoading> with SingleTickerProviderStateMixin {
  late Animation<int> animation;
  late AnimationController controller;
  var pointsArc = [0, 45, 90, 135, 180, 225, 270, 315];
  @override
  void initState() {
    super.initState();
    controller =
        AnimationController(duration: const Duration(seconds: 1), vsync: this);
    animation = IntTween(begin: 0, end: 60).animate(controller)
      ..addListener(() {
        setState(() {
          if (animation.value % 10 == 0) {
            //数组移位
            var t = pointsArc[0];
            pointsArc.removeAt(0);
            pointsArc.add(t);
          }
        });
      })
      ..addStatusListener((status) {
        // 动画状态监听
        if (status == AnimationStatus.completed) {
          controller.repeat();
        }
      });
    controller.forward();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        width: widget.style.width,
        height: widget.style.height,
        child: LoadingAnimation(
            animation: animation,
            pointsArc: pointsArc,
            loadingStyle: widget.style));
  }
}
